﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Catalog
{
    public partial class frmThuenha : DevExpress.XtraEditors.XtraForm
    {
        DAL.Entities_DAL.KH_CH_DAL _dal = new DAL.Entities_DAL.KH_CH_DAL();
        DAL.Entities_DAL.Canho_DAL _ch = new DAL.Entities_DAL.Canho_DAL();
        DAL.Entities_DAL.Khachhang_DAL _kh = new DAL.Entities_DAL.Khachhang_DAL();
        DAL.Entities_DAL.BGDichvu_DAL _bgdv = new DAL.Entities_DAL.BGDichvu_DAL();
        DAL.Entities_DAL.Chungcu_DAL _cc = new DAL.Entities_DAL.Chungcu_DAL();
        BLL.Entities_BLL.BGDichvu_BLL _blldv = new BLL.Entities_BLL.BGDichvu_BLL();

        public frmThuenha()
        {
            InitializeComponent();
        }

        protected DataTable _dtb = new DataTable("canho");
        protected DataTable _dtbb = new DataTable("ch");
        protected DataTable _okey = new DataTable("bang");
        protected DataTable _bg = new DataTable("banggia");
        protected bool IsAdd { set; get; }
        protected bool IsEdit { set; get; }

        private void frmThuenha_Load(object sender, EventArgs e)
        {
            load_canho();
            LoadData();
            ClearDataBindings();
            DataBindingControl();
            //ClearDataBindings_CH();
            //DataBindingControl_CH();
            //load can ho k dc thue
            DataTable _chkt = _ch.load_can_ho_khong_thue();
            cbCh.Properties.DataSource = _chkt;
            //load khach hang
            DataTable _loadkh = _kh.Select_kh();
            cbKh.Properties.DataSource = _loadkh;

            //load chung cu

            DataTable _bgcc = _ch.Select_Chungcu();
            cbCc.Properties.DataSource = _bgcc;
        }

        //load can ho

        void load_canho()
        {
            lvBan.Items.Clear();
            _dtb = _ch.load_can_ho();
            DataTable _kh_ch = _dal.Select();

            foreach (DataRow drw in _dtb.Rows)
            {
                ListViewItem lvwitem = new ListViewItem();
                ListViewItem lvwitem2 = new ListViewItem();
                lvwitem.Text = drw[0].ToString();
                lvwitem.Tag = drw[0].ToString();
                int hinh = 0;
                foreach (DataRow row in _kh_ch.Rows)
                {               
                    if (row["MA_CH"].ToString() == lvwitem.Tag.ToString() && row["NGAY_THUE"].ToString() != null)
                    {
                        lvwitem.ImageIndex = 1;
                        hinh = 1;                    
                    }
                }
                if (hinh == 0)
                    lvwitem.ImageIndex = 0;
                lvBan.Items.Add(lvwitem);
            }
        }

        //load data bg_dv

        void LoadData()
        {
             _bg = _bgdv.Select();

            if (_bg != null)
            {
                dsDv.DataSource = _bg;
            }
        }
        //
        void ClearDataBindings()
        {
            txtMadv.DataBindings.Clear();
            txtTendv.DataBindings.Clear();
            txtDongia.DataBindings.Clear();
        }
        //
        void DataBindingControl()
        {
            txtMadv.DataBindings.Add("EditValue", _bg, ".MA_BGDV");
            txtTendv.DataBindings.Add("EditValue", _bg, ".TEN_DV");
            txtDongia.DataBindings.Add("EditValue", _bg, ".DON_GIA");
        }

        //doi chu cai dau thanh chu hoa

        public string Proper(string name)
        {
            string s = name.Trim();
            string result = "";

            string tmp = ""; if (s != "") tmp = s.Substring(0, 1);
            result += tmp.ToUpper();
            for (int i = 1; i < s.Length; i++)
            {
                if ((s[i - 1].ToString() == " ") && (s[i].ToString() != " "))
                {
                    string ss = s[i].ToString();
                    result += ss.ToUpper();
                }
                else
                {
                    string ss = s[i].ToString();
                    result += ss.ToLower();

                }
            }

            return result;
        }

        private void cmdLammoi_Click(object sender, EventArgs e)
        {
            var name = Proper(txtTendv.Text);


            if (IsAdd)
            {
                var o = new DAL.Entities.BGDichvu()
                {
                    MA_BGDV = txtMadv.Text,
                    TEN_DV  = name,
                    DON_GIA = txtDongia.Text
                };

                var oki = _bgdv.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadData();
                    IsAdd = false;
                    PerformRefresh();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);               
                }

            }

            if (IsEdit)
            {
                var o = new DAL.Entities.BGDichvu()
                {
                    MA_BGDV = txtMadv.Text,
                    TEN_DV = name,
                    DON_GIA = txtDongia.Text
                };

                var oki = _bgdv.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadData();
                    IsEdit = false;
                    PerformRefresh();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);                  
                }
            }
        }

        private void cmdThêm_Click(object sender, EventArgs e)
        {
            DataTable _ma = _bgdv.Select_max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _blldv.AddID(_ma.Rows[0]["id"] + "");
                    txtMadv.Text = ma;

                    ClearDataBindings();
                    txtTendv.Text = null;
                    txtDongia.Text = null;
                    txtTendv.Focus();
                }
            IsAdd = true;
        }

        private void cmdSua_Click(object sender, EventArgs e)
        {
            IsEdit = true;
            txtTendv.Focus();
        }

        private void cmdLoad_Click(object sender, EventArgs e)
        {
            LoadData();

            if (_bg != null)
            {
                // Binding data
                ClearDataBindings();
                if (_bg.Rows.Count > 0) DataBindingControl();
            }
        }
        //refresh

        void PerformRefresh()
        {
            LoadData();

            if (_bg != null)
            {
                // Binding data
                ClearDataBindings();
                if (_bg.Rows.Count > 0) DataBindingControl();
            }
        }

        private void cmdXoa_Click(object sender, EventArgs e)
        {
            string id = txtMadv.Text;

            DialogResult result;
            var ok = _bgdv.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //bingdings can ho

        void ClearDataBindings_CH()
        {
            cbDt.DataBindings.Clear();
            cbCc.DataBindings.Clear();
            cbDap.DataBindings.Clear();
            cbHoi.DataBindings.Clear();
            cbMay.DataBindings.Clear();
            cbTT.DataBindings.Clear();
            txtLo.DataBindings.Clear();
            txtMach.DataBindings.Clear();
        }
        void DataBindingControl_CH()
        {
            txtMach.DataBindings.Add("EditValue", _dtbb, ".MA_CH");
            cbCc.DataBindings.Add("EditValue", _dtbb, ".MA_CC");
            cbDt.DataBindings.Add("EditValue", _dtbb, ".DIEN_TICH");
            cbDap.DataBindings.Add("EditValue", _dtbb, ".SO_LUONG_XE_DAP");
            cbHoi.DataBindings.Add("EditValue", _dtbb, ".SO_LUONG_XE_HOI");
            cbMay.DataBindings.Add("EditValue", _dtbb, ".SO_LUONG_XE_MAY");
            cbTT.DataBindings.Add("EditValue", _dtbb, ".TRANG_THAI");
            txtLo.DataBindings.Add("EditValue", _dtbb, ".LO");
        }

        private void cbCh_EditValueChanged(object sender, EventArgs e)
        {
            _dtbb = _ch.tk_canho(cbCh.EditValue + "");
            ClearDataBindings_CH();
            DataBindingControl_CH();
            //tinh iten thuê nha
            txtGthue.Text = ((Convert.ToDecimal(cbDt.Text)*100000 + 500000 + 300000 + 200000)).ToString();
        }

        private void lvBan_Click(object sender, EventArgs e)
        {
            try
            {
                string xx = null;
                _dtb = _ch.Select();
                ListViewItem lvwitem = new ListViewItem();
                lvwitem = lvBan.SelectedItems[lvBan.SelectedItems.Count - 1];
                if (lvwitem.ImageIndex == 1 || lvwitem.ImageIndex == 0)
                {
                    foreach (DataRow drw in _dtb.Rows)
                    {
                        xx = drw["ma_ch"].ToString();
                        if (drw["ma_ch"].ToString() == lvwitem.Tag.ToString())
                        {
                            DataTable _hien = _dal.Select(drw["ma_ch"].ToString());

                            foreach (DataRow row1 in _hien.Rows)
                            {

                                if (row1["ngay_thue"].ToString() != null)
                                {
                                   
                                }
                                else
                                { 
                                
                                }
                            }
                            
                        }
                        mach.Text = xx;
                    }
                    
                }
              
            }
            catch
            {

            }
        }

    }
}